package com.mogujie.tt.ui.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mogujie.tt.DB.sp.GesturespasswordSp;
import com.mogujie.tt.DB.sp.LoginSp;
import com.mogujie.tt.R;
import com.mogujie.tt.app.IMApplication;
import com.mogujie.tt.imservice.event.OpenpwdEvent;
import com.mogujie.tt.imservice.support.IMServiceConnector;
import com.mogujie.tt.ui.activity.CreateGesturePasswordActivity;
import com.mogujie.tt.ui.activity.NumPwdActivity;
import com.mogujie.tt.ui.activity.OpenPwdActivity;
import com.mogujie.tt.ui.base.TTBaseFragment;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2015/10/23.
 */
public class GesturesFragment extends TTBaseFragment implements View.OnClickListener{
    private View curView = null;
    private View openpwd;
    private View replacepwd;
    private PopupWindow mpopupWindow;
    private TextView OpenPwdTx,ChangePwdTx;
    private boolean N=false;
    private boolean G=false;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle bundle) {
        if (null != curView) {
            ((ViewGroup) curView.getParent()).removeView(curView);
            return curView;
        }
        curView = inflater.inflate(R.layout.tt_fragment_gestures, topContentView);
        initRes();
        return curView;
    }
    private void initRes() {
        // 设置标题栏
        setTopTitle("密码锁");
        setTopLeftButton(R.drawable.tt_top_back);
        topLeftContainerLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                getActivity().finish();
            }
        });
        setTopLeftText(getResources().getString(R.string.top_left_back));
        initView();
    }

    private void initView() {

        EventBus.getDefault().register(this);
        OpenPwdTx = (TextView) curView.findViewById(R.id.OpenPwdTx);

        openpwd = curView.findViewById(R.id.openpwd);
        replacepwd = curView.findViewById(R.id.replacepwd);
        ChangePwdTx = (TextView) curView.findViewById(R.id.ChangePwdTx);
        listenView();
    }

    private void listenView() {

        String sign=GesturespasswordSp.get(getActivity(),GesturespasswordSp.getNUM(),"");

        if ("".equals(sign)||sign==null){
            N=false;
        }else{
            if (sign.trim().length()==6){
                N=true;
            }else
                N=false;
        }
        if (IMApplication.getInstance().getLockPatternUtils().savedPatternExists()){
            G = true;
        }else
            G=false;

        if (N||G){
            replacepwd.setEnabled(true);
            ChangePwdTx.setTextColor(0xff000000);
            OpenPwdTx.setText("关闭密码");
        }else{
            replacepwd.setEnabled(false);
            ChangePwdTx.setTextColor(0xffbebebe);
            OpenPwdTx.setText("打开密码");
        }
        System.out.println("111111111" + N + "      " + G);

        openpwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (N||G){

                    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light_Dialog));
                    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    View dialog_view = inflater.inflate(R.layout.tt_custom_dialog1, null);
                    TextView textText = (TextView) dialog_view.findViewById(R.id.dialog_title);
                    textText.setText("关闭密码");
                    TextView textView1 = (TextView) dialog_view.findViewById(R.id.dialog_sm);
                    textView1.setText("是否关闭密码？");
                    builder.setView(dialog_view);
                    builder.setPositiveButton(getString(R.string.tt_ok), new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            GesturespasswordSp.set(getActivity(), GesturespasswordSp.getNUM(), "");
                            IMApplication.getInstance().getLockPatternUtils().Lockdelete();
                            replacepwd.setEnabled(false);
                            OpenPwdTx.setText("打开密码");
                            listenView();
                            dialog.dismiss();
                        }
                    });

                    builder.setNegativeButton(getString(R.string.tt_cancel), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
                    builder.show();
                }else
                    startActivity(new Intent(getActivity(), OpenPwdActivity.class));
            }
        });

        replacepwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopMenu();
            }
        });

    }

    public void onEventMainThread(OpenpwdEvent event) {
        switch (event) {
            case SUCCEED:
                getActivity().finish();
                EventBus.getDefault().unregister(this);
                break;
            case BE_DEFEATER:
                break;
        }
    }

    private void showPopMenu() {
        View view = View.inflate(getActivity(), R.layout.share_popup_menu, null);
        RelativeLayout rl_weixin = (RelativeLayout) view.findViewById(R.id.rl_weixin);
        RelativeLayout rl_weibo = (RelativeLayout) view.findViewById(R.id.rl_weibo);
        Button bt_cancle = (Button) view.findViewById(R.id.bt_cancle);

        rl_weixin.setOnClickListener(this);
        rl_weibo.setOnClickListener(this);
        bt_cancle.setOnClickListener(this);

        view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mpopupWindow.dismiss();
            }
        });

        view.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in));
        LinearLayout ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);
        ll_popup.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.push_bottom_in));

        if(mpopupWindow==null){
            mpopupWindow = new PopupWindow(getActivity());
            mpopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            mpopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            mpopupWindow.setBackgroundDrawable(new BitmapDrawable());

            mpopupWindow.setFocusable(true);
            mpopupWindow.setOutsideTouchable(true);
        }

        mpopupWindow.setContentView(view);
        mpopupWindow.showAtLocation(openpwd, Gravity.BOTTOM, 0, 0);
        mpopupWindow.update();
    }

    @Override
    protected void initHandler() {

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.rl_weixin:
                startActivity(new Intent(getActivity(),CreateGesturePasswordActivity.class));
                mpopupWindow.dismiss();
                break;
            case R.id.rl_weibo:
                startActivity(new Intent(getActivity(),NumPwdActivity.class));
                mpopupWindow.dismiss();
                break;
            case R.id.bt_cancle:

                mpopupWindow.dismiss();
                break;
        }
    }
}
